package com.spd.id2.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.button.MaterialButton;
import com.google.gson.Gson;
import com.spd.id2.impl.CardInfo;
import com.spd.id2.impl.IIdReadCallBack;
import com.spd.id2.impl.PowerEntity;
import com.spd.lib.mvp.BaseMvpActivity;

/**
 * @author :Reginer  2022/8/10 0010 15:57.
 * 联系方式:QQ:282921012
 * 功能描述:
 */
public class ReadId2Activity extends BaseMvpActivity<Id2Presenter> implements Id2Contract.View, IIdReadCallBack {
    private long initStartOrLastReadTime = System.currentTimeMillis();

    public long successCount = 0;
    public MaterialButton clearUi;
    TextView readId2Log;
    TextView idInfo;
    ImageView idPhoto;
    private TextView config;


    @Override
    public void onId2Check(boolean check) {
        try {
            readId2Log.setText(TextUtils.concat("初始化校验", check ? "成功" : "失败", "耗时:" + (System.currentTimeMillis() - initStartOrLastReadTime) + "ms"));
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("spdId2 ", Log.getStackTraceString(e));
        }

    }

    @Override
    public void onReadId2(CardInfo cardInfo) {
        try {
            PlaySoundUtils.play(1);
            idInfo.setText(TextUtils.concat(
                    "姓名:", cardInfo.getName(), "\n",
                    "身份证号:", cardInfo.getCardNum(), "\n",
                    "性别:", cardInfo.getSex(), "\n",
                    "民族:", cardInfo.getNation(), "\n",
                    "住址:", cardInfo.getAddress(), "\n",
                    "出生:", cardInfo.getBirth(), "\n",
                    "签发机关:", cardInfo.getSignOrgan(), "\n",
                    "有效期限:", cardInfo.getStartTime(), "-", cardInfo.getEndTime()
            ));
            idPhoto.setImageBitmap(cardInfo.getPhoto());
            successCount = successCount + 1;
            readId2Log.setText(TextUtils.concat("耗时:", cardInfo.getReadTime() - initStartOrLastReadTime + "ms", "成功次数:" + successCount));
        } catch (Exception e) {
            e.printStackTrace();
        }
        initStartOrLastReadTime = System.currentTimeMillis();
    }

    @Override
    protected Id2Presenter createPresenter() {
        return new Id2Presenter();
    }

    @Override
    protected int getActLayoutId() {
        return R.layout.activity_read_id2;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PlaySoundUtils.initSoundPool(this);
        mPresenter.loadModuleConfig();
    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        mToolBar.setNavigationIcon(null);
    }

    @Override
    public void onBackPressed() {
        mPresenter.releaseDev();
        super.onBackPressed();
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        clearUi = findViewById(R.id.clearUi);
        readId2Log = findViewById(R.id.readId2Log);
        idInfo = findViewById(R.id.idInfo);
        idPhoto = findViewById(R.id.idPhoto);
        config = findViewById(R.id.config);
        clearUi.setOnClickListener(v -> mPresenter.clearUi());
    }

    @Override
    public void onLoadProgress() {
        showProgress();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onLoadConfigError() {
        dismissProgress();
        config.setText("获取配置失败");
        readId2Log.setText("型号:"+mPresenter.getRealModule()+"未获取到配置文件");

    }

    @Override
    public void onLoadConfigSuccess(@NonNull PowerEntity id2Entity) {
        dismissProgress();
        initStartOrLastReadTime = System.currentTimeMillis();
        Log.i("spdId2", "id2Entity:" + id2Entity);
        config.setText(id2Entity.toString());
        mPresenter.initDev(id2Entity, this);
    }

}
